package com.zking.forward.shopping;

import com.zking.dao.ShopCarDao;
import com.zking.entity.User;
import com.zking.util.RedirectMsg;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/shopcarAdd")
public class ShopcarAdd extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理请求的编码，解决中文乱码
        request.setCharacterEncoding("utf-8");
        //处理响应的编码 ，解决中文乱码
        response.setContentType("text/html;charset=utf-8");

        System.out.println("ShopcarAdd===进来");

        int id = Integer.parseInt(request.getParameter("id"));
        User user = (User)request.getSession().getAttribute("user");
        if(user==null){
            response.sendRedirect("shopping.jsp");
            return;
        }
        // 对没登陆情况的处理
//        if(user==null){
//            request.setAttribute("msg", RedirectMsg.notLogin);
//            request.setAttribute("pageName", RedirectMsg.loginPageName);
//            request.setAttribute("url", RedirectMsg.loginUrl);
//            request.getRequestDispatcher("redirect.jsp").forward(request, response);
//            return;
//        }

        String eu_user_id = user.getEu_user_id();
        boolean flag = ShopCarDao.addProduct(eu_user_id, id);
        if(flag){//添加购物车成功
            //加入购物车之前页面还是停留在商品详情的页面
            response.sendRedirect(" productSelectById?id="+id);
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
